package com.china08.yxyapi.api.manage.edu.platform.lesson;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLessonItem;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLessonItemQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemQuesRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;
import com.china08.yxyapi.util.StringTrimUtil;
import com.china08.yxyapi.util.YxyKeChengUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class EduLessonItemDetailController extends YxyApiBaseController {
    @Autowired
    YxyKeChengUtil YxyKeChengUtil;

    @Autowired
    YxyLessonsCloudLessonRepository lessonRepository;

    @Autowired
    YxyLessonsCloudLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsCloudLessonItemQuesRepository itemQuesRepository;

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/edu/platform/lesson/item/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String itemId = reqModel.itemId;
        String userId = getProfile(request, response).getId();
        boolean userVip = Boolean.parseBoolean(getProfile(request, response).getUserVip());
        RespModel respModel = new RespModel();
        String location = "https://yxyres.hrbeduy.com/converted/mp4/";

        YxyLessonsCloudLessonItem item = itemRepository.findById(itemId);
        List<YxyLessonsCloudLessonItemQues> itemQueses = itemQuesRepository.findByLessonItemId(itemId);

        if (item != null) {
            respModel.name = item.getTitle();
            respModel.intro = item.getIntro();
            respModel.status = item.getStatus();
            respModel.rejectReason = item.getRejectReason();
            String videoId = item.getVideoId();
            if (videoId != null) {
                YxyLessonsVideo video = videoRepository.findById(videoId);
                respModel.videoStatus = video.getStatus();
                respModel.duration = StringTrimUtil.disDuration(video.getDuration());

                // 获取视频播放路径
                // ####
                // 获取视频播放路径
                // 视频的md5
                String md5 = video.getMd5();
                // md5前3位
                String s03 = md5.substring(0, 3);
                // md5前3-6位
                String s36 = md5.substring(3, 6);
                // 后缀
                String suffix = ".mp4?&lessonsCloud_sign=" + UUID.randomUUID().toString().replace("-", "");

                respModel.videoPath = location + s03 + File.separator + s36 + File.separator + md5 + suffix;
            }
            respModel.upName = "";
            respModel.endName = "";

            // 获取上一课时
            Integer orderNum = item.getOrderNum();
            String lessonId = item.getLessonId();
            YxyLessonsCloudLessonItem upItem = itemRepository.findTop1ByLessonIdAndStatusNotAndOrderNumLessThanOrderByOrderNumDesc(lessonId, -1, orderNum);
            if (upItem != null) {
                respModel.upName = upItem.getTitle();
                respModel.upItemId = upItem.getId();
            }
            upItem = itemRepository.findTop1ByLessonIdAndStatusNotAndOrderNumGreaterThanOrderByOrderNumAsc(lessonId, -1, orderNum);
            if (upItem != null) {
                respModel.endName = upItem.getTitle();
                respModel.endItemId = upItem.getId();
            }
            // 获取课程封面
            YxyLessonsCloudLesson lesson = lessonRepository.findById(lessonId);
            if (lesson != null) {
                respModel.lessonCover = lesson.getCover();
            }
        }
        List<String> quesIdList = new ArrayList<String>();
        for (YxyLessonsCloudLessonItemQues itemQues : itemQueses) {
            quesIdList.add(itemQues.getQuesId());
        }
        respModel.quesIdList = quesIdList;
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String itemId;
    }

    public static class RespModel extends YxyApiRespModel {
        public String name;
        public String intro;
        public Integer status;
        public Integer videoStatus = 0;
        public String rejectReason;
        public String videoId;
        public String upItemId;
        public String upName;
        public String endItemId;
        public String endName;
        public String videoPath = "";
        public String duration = "";
        public String lessonCover = "";
        public List<String> quesIdList;
    }
}
